package com.elitel.frame.business.controller;


import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elitel.common.exception.BaseException;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.base.component.RedisCacheComponent;
import com.elitel.frame.base.service.CensusService;
import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.business.service.CensusSearchService;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.ext.ReturnData;
import com.elitel.frame.main.entity.vo.JsonDataResult;
import com.elitel.frame.main.entity.vo.JsonResult;


@Controller
@RequestMapping("/business/censusTab")
public class CensusSearchController {

	@Resource
	private CensusSearchService censusSearchServiceImpl;
	
	@Resource
	private CensusService censusServiceImpl;
	
	@Resource
	private CfgDbService cfgDbService;
	
	@Resource
	private RedisCacheComponent redisCacheComponent;
//	@Resource
//	private RedisCacheService redisCacheService;
	
//	@Autowired
//    private JmsTemplate jmsTemplate;
//	@Resource(name = "serverQueueDestination")
//	private Destination destination;
//	@Autowired
//    private AmqpTemplate amqpTemplate;
	
	//private static final RedisDbIndex rdi =RedisDbIndex.ZERO;


	private static final Logger logger = LoggerFactory.getLogger(CensusSearchController.class);

	
	@RequestMapping("/getCensusTable")
	@ResponseBody
	public Object getCensusSearch(String serviceID,String conQuery){
		String xx  = censusServiceImpl.getDbKeyes(serviceID);
		CfgDb cfgDb = cfgDbService.selectByKey(xx);
		Map<String, Object> map = censusSearchServiceImpl.getCensusSearch(cfgDb,serviceID,conQuery);
		return map;
	}
	
	//根据服务id获取横坐标标题和纵坐标标题
	@RequestMapping("/selectXYMap")
	@ResponseBody
	public Object selectXYMap(HttpServletRequest request){
		JsonResult jsonResult = new JsonResult();
		String id = "";
		try {
			id = request.getParameter("serviceID");
			String dbKey = "CENSUS_XY_"+id;
//			if(redisCacheService.stringExists(dbKey, rdi)){
//				
//			}else{
				Map<String, Object> map = censusServiceImpl.selectXYMap(id);
				jsonResult.setData(map);
				jsonResult.setMsg("查询成功");
				jsonResult.setSuccess(true);
//			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMsg("查询失败");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}
	//根据服务id和查询条件查询
	@RequestMapping("/getCensusTableing")
	@ResponseBody
	public 	Object getCensusTableing(HttpServletRequest request){
		JsonResult jsonResult = new JsonResult();
		String conString ="";
		String serviceString="";
		Map<String, Object> map =null;
		try {
			if (request.getParameter("conQuery")!=null) {
				conString = request.getParameter("conQuery");
			}
			 serviceString = request.getParameter("serviceID");
			 String xx  = censusServiceImpl.getDbKeyes(serviceString);
			 CfgDb cfgDb = cfgDbService.selectByKey(xx);
			 map= censusSearchServiceImpl.getCensusTableed(cfgDb,serviceString,conString);
			 jsonResult.setData(map);
			 jsonResult.setMsg("查询成功");
			 jsonResult.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMsg("查询失败");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}

	//获取标准格式类
	@RequestMapping("/getTableColumnBiao")
	@ResponseBody
	public 	Object getTableColumnBiao(HttpServletRequest request){
		String id="";
		Map<String, Object> map  = null;
		List<ReturnData> list = null;
		JsonResult jsonResult = new JsonResult();
		try {
			 id = request.getParameter("serviceID");
			 list = censusServiceImpl.getTableColumnBiao(id);
			 jsonResult.setMsg("查询成功");
			 jsonResult.setSuccess(true);
			 jsonResult.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMsg("查询失败");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}
	
	@RequestMapping("/getSelectCensusTableColumns")
	@ResponseBody
	public 	Object getSelectCensusTableColumns(HttpServletRequest request){
		JsonResult jsonResult = new JsonResult();
		String id="";
		Map<String, Object> map = null;
		try {
			 id = request.getParameter("serviceID");
			 map = censusServiceImpl.getSelectCensusTableColumns(id);
			 jsonResult.setMsg("查询成功");
			 jsonResult.setSuccess(true);
			 jsonResult.setData(map);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMsg("查询失败");
			jsonResult.setSuccess(false);
		}
		return map;
	}
	
	@RequestMapping(value="/getCensusTableColumn",method=RequestMethod.GET)
	@ResponseBody
	public 	Object getCensusTableColumn(HttpServletRequest request){
		String id=request.getParameter("serviceID");
		Map<String, Object> map = null;
		JsonResult jsonResult = new JsonResult();
		try {
			 map = censusServiceImpl.getCensusColumn(id);
			 jsonResult.setMsg("查询成功");
			 jsonResult.setSuccess(true);
			 jsonResult.setData(map);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMsg("查询失败");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}
	
	
	@RequestMapping(value="/indexText")
	@ResponseBody
	public Object getIndexTest(Boolean debug,HttpServletRequest request){
		String uuid = UUID.randomUUID().toString();
		//saveActivemqOne(uuid);
		//String debug = request.getParameter("debug");
		JsonDataResult jsonDataResult = new JsonDataResult();
		CfgDb cfgDb = new CfgDb();
		String sqlString = "";
		Object object = new Object();
		try {
			List<Map<String, Object>> list = censusSearchServiceImpl.executequerySqls(uuid,cfgDb, sqlString);
			jsonDataResult.setData(list);
			jsonDataResult.setMessage("查询成功");
			jsonDataResult.setResult(0);
		} catch (BaseException e) {
			e.printStackTrace();
			jsonDataResult.setMessage(e.getExceptionVO().getDesc());
			jsonDataResult.setResult(Integer.valueOf(e.getExceptionVO().getValue()));
		}
		if (debug) {
		//if (debug!=null&&Integer.valueOf(debug)==1) {
//			Object text = amqpTemplate.receiveAndConvert("serverQueueDestination");
//			object = ActiveMQUtil.showInformation(jmsTemplate,destination,uuid);
			//object = ActiveMQUtil.showInformation(jmsTemplate,destination,"121");
			//object = showInformation(uuid);
//			System.out.println("最后一次输出"+uuid);
			return object;
		}else{
			return jsonDataResult;
		}
		
	}

	public void run() {
		System.out.println("数据转换任务线程开始执行");
	}
}
